package com.job.learn.cuiyt.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.job.learn.cuiyt.dto.FileItem;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
/**
 * @author: cuiyutian1
 * @date: 2021/12/26 12:15
 * @description:
 */
@Component
public class FileBackUpJob implements SimpleJob {

    private static ArrayList<FileItem> fileList = new ArrayList<FileItem>();

    /**
     * 执行任务
     * @param shardingContext
     */
    @Override
    public void execute(ShardingContext shardingContext) {
        System.out.println("分片："+ shardingContext.getShardingItem());
        ArrayList<FileItem> fileList = getFileList();
        backUpFiles(fileList);
    }

    private void backUpFiles(ArrayList<FileItem> fileList) {
        if(CollectionUtils.isEmpty(fileList)){
            return;
        }

        for (FileItem item : fileList) {
            System.out.println("开始备份：" +  item.toString());
            item.setBackUp(true);
        }
    }

    private ArrayList<FileItem> getFileList() {
        for (int i = 0; i < 100; i++) {
            FileItem fileItem = new FileItem();
            fileItem.setId(i);
            fileList.add(fileItem);
        }
        return fileList;
    }

}
